package assignment.ton.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import assignment.ton.def.Action;
import assignment.ton.def.Class;
import assignment.ton.def.Util;
import assignment.ton.gui.AboutFrame;
import assignment.ton.gui.ClassFrame;
import assignment.ton.gui.HelpFrame;
import assignment.ton.gui.MainFrame;
import assignment.ton.gui.MethodFrame;
import assignment.ton.gui.VariableFrame;
/**
 * Action listener for the menu, simply sets the action for when the draw panel is clicked
 * @author Tim
 */
public class MenuActionListener implements ActionListener{
	private MainFrame mainFrame;
	public MenuActionListener(MainFrame mF){
		mainFrame = mF;
	}
	public void actionPerformed(ActionEvent event){
		String actionCommand = event.getActionCommand();
		if(Util.canCancel(mainFrame.getAction())){
			if(actionCommand.equals("Quit")){
				Util.quit();
			}else if(actionCommand.equals("Cancel")){
				if(Util.canCancel(mainFrame.getAction())){
					mainFrame.setAction(Action.NONE);
					if(mainFrame.getSelected()!=null){
						mainFrame.getSelected().setColor(new Color(0,205,255));
						mainFrame.isSelected(null);
						mainFrame.getDrawPanel().repaint();
					}
				}
			}else if(actionCommand.equals("Add Class")){
				mainFrame.setAction(Action.ADDCLASS);
			}else if(actionCommand.equals("Add Link")){
				mainFrame.setAction(Action.ADDLINK);
			}else if(actionCommand.equals("Delete Link")){
				mainFrame.setAction(Action.DELETELINK);
			}else if(actionCommand.equals("Edit Link")){
				mainFrame.setAction(Action.EDITLINK);
			}else if(actionCommand.equals("Variables")){
				mainFrame.setAction(Action.VARIABLES);
				Class c = mainFrame.getSelected();
				if(c!=null){
					VariableFrame f = new VariableFrame(c, mainFrame);
					f.setVisible(true);
				}
			}else if(actionCommand.equals("Methods")){
				mainFrame.setAction(Action.METHODS);
				MethodFrame m = new MethodFrame(mainFrame.getSelected(), mainFrame);
				m.setVisible(true);
			}else if(actionCommand.equals("Edit Class")){
				mainFrame.setAction(Action.EDITCLASS);
				ClassFrame cF = new ClassFrame(mainFrame.getSelected(), mainFrame.getDrawPanel(), mainFrame);
				cF.setVisible(true);
			}else if(actionCommand.equals("Delete Class")){
				if(Util.areYouSure()){ 
					mainFrame.getDrawPanel().removeClass(mainFrame.getSelected().getName()); 
					mainFrame.getDrawPanel().repaint();
					mainFrame.isSelected(null);
					mainFrame.setAction(Action.NONE);
				}
			}else if(actionCommand.equals("Export")){
				mainFrame.export();
			}else if(actionCommand.equals("Undo")){
				mainFrame.getHistObj().undo();
			}else if(actionCommand.equals("Redo")){
				mainFrame.getHistObj().redo();
			}else if(actionCommand.equals("New")){
				mainFrame.getHistObj().newStart();
			}else if(actionCommand.equals("Open")){
				mainFrame.getHistObj().open();
			}else if(actionCommand.equals("Save")){
				mainFrame.getHistObj().save();
			}else if(actionCommand.equals("Print")){
				mainFrame.getDrawPanel().printMe();
			}else if(actionCommand.equals("About")){
				AboutFrame a = new AboutFrame();
				a.setVisible(true);
			}else if(actionCommand.equals("Index")){
				/*String htmlFilePath = "path/to/html/file.html";
				File htmlFile = new File(htmlFilePath);
				try {
					Desktop.getDesktop().browse(htmlFile.toURI());
				} catch (IOException e) {
					e.printStackTrace();
				}*/
				HelpFrame hf = new HelpFrame();
				hf.setVisible(true);
			}
		}
	}
}
